/********************************************************************************
 * ListingOptionsAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 8, 2008 2:01:42 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ListingOptionsServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.Itemtype;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.dto.ListingOptions;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.util.AdminConstants;

public class ListingOptionsAction  extends AdminSystemSetup {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ListingOptionsAction.class);
	/**  */
	private static final long serialVersionUID = 1L;

	private ListingOptions listingOptions = new ListingOptions();

	private List<Itemtype> homepageFeaturedListsSettingsList = new ArrayList<Itemtype>();

	private List<Languages> languagesList = new ArrayList<Languages>();

	private List<LocationCategories> regionList = new ArrayList<LocationCategories>();

	private List<LocationCategories> countryList = new ArrayList<LocationCategories>();

	private List<LocationCategories> locationList = new ArrayList<LocationCategories>();

	/**
	 * Gets the listing options.
	 *
	 * @return the listing options
	 */

	private Itemtype homepageFeaturedListsSettings;
	/**
	 * Sets the listing options.
	 *
	 * @param listingOptions the new listing options
	 */

	private Map<Integer, Itemtype> myHashmapSettingsMap;

	private int featuredColumn;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		listingOptions = ListingOptionsServiceDelegator.getListingOptions();

		languagesList = TranslationSystemServiceDelegator.getList();
		getLocation(regionList, AdminConstants.ONE_NUMBER, AdminConstants.ZERO_NUMBER);
		getLocation(countryList, AdminConstants.TWO_NUMBER, listingOptions.getDefaultRegion());

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String saveListingOptions() {
		try {
			ListingOptionsServiceDelegator.updateListingOptions(listingOptions);
		} catch (HibernateException e) {
			addActionError("Input valid Data.");
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD
					+ e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public String showHomePageListSettings() {
		if (logger.isDebugEnabled()) {
			logger.debug("showHomePageListSettings() - start"); //$NON-NLS-1$
		}
		myHashmapSettingsMap = new HashMap<Integer, Itemtype>();
		this.homepageFeaturedListsSettingsList = (List<Itemtype>)CommonFunctionDelegator.getAllfromtable("com.printeverywhere.dto.Itemtype");
		for (Itemtype homepageFeaturedListsSettings : this.homepageFeaturedListsSettingsList) {
			featuredColumn = homepageFeaturedListsSettings.getHomepageColumns();
			this.myHashmapSettingsMap.put(homepageFeaturedListsSettings.getId(),
					homepageFeaturedListsSettings);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("showHomePageListSettings() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	/**
	 * Updates homepageFeaturedLists values.
	 *
	 * @return SUCCESS if the value is updated successfully else INPUT if any
	 *         error is there.
	 */
	public String updateHomepageListsSettings() {
		try {
			for (Itemtype homepageFeaturedListsSettings : this.myHashmapSettingsMap.values()) {
				homepageFeaturedListsSettings.setHomepageColumns(featuredColumn);
				ListingOptionsServiceDelegator.updateHomepageListSettings(homepageFeaturedListsSettings);
			}
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_UPDATING_THE_RECORD + e.getMessage());
			addActionError("Input valid Data");
			return INPUT;
		}
		return SUCCESS;
	}

	public void getLocation(List<LocationCategories> list, int level, int categoryId){
		locationList = (List<LocationCategories>) ActionContext.getContext().getApplication().get(AdminConstants.LOCATIONCATEGORIES);
		list.clear();
		for (Iterator iterator = locationList.iterator(); iterator
				.hasNext();) {
			LocationCategories locationCategories = (LocationCategories) iterator
					.next();

				if(categoryId > 0){
					if(locationCategories.getLocationCategories().getCategoryId() == categoryId){
						if (locationCategories.getLevel() == level){
							list.add(locationCategories);
						}
					}
				}else {
					if (locationCategories.getLevel() == level){
						list.add(locationCategories);
					}
				}
		}
	}

	public ListingOptions getListingOptions() {
		return listingOptions;
	}

	public void setListingOptions(ListingOptions listingOptions) {
		this.listingOptions = listingOptions;
	}

	public List<Itemtype> getItemtypeList() {
		return homepageFeaturedListsSettingsList;
	}

	public void setItemtypeList(
			List<Itemtype> homepageFeaturedListsSettingsList) {
		this.homepageFeaturedListsSettingsList = homepageFeaturedListsSettingsList;
	}

	

	/**
	 * @return the homepageFeaturedListsSettings
	 */
	public Itemtype getHomepageFeaturedListsSettings() {
		return homepageFeaturedListsSettings;
	}

	/**
	 * @param homepageFeaturedListsSettings the homepageFeaturedListsSettings to set
	 */
	public void setHomepageFeaturedListsSettings(Itemtype homepageFeaturedListsSettings) {
		this.homepageFeaturedListsSettings = homepageFeaturedListsSettings;
	}

	public Map<Integer, Itemtype> getMyHashmapSettingsMap() {
		return myHashmapSettingsMap;
	}

	public void setMyHashmapSettingsMap(
			Map<Integer, Itemtype> myHashmapSettingsMap) {
		this.myHashmapSettingsMap = myHashmapSettingsMap;
	}

	public int getFeaturedColumn() {
		return featuredColumn;
	}

	public void setFeaturedColumn(int featuredColumn) {
		this.featuredColumn = featuredColumn;
	}

	public List<Languages> getLanguagesList() {
		return languagesList;
	}

	public void setLanguagesList(List<Languages> languagesList) {
		this.languagesList = languagesList;
	}

	public List<LocationCategories> getRegionList() {
		return regionList;
	}

	public void setRegionList(List<LocationCategories> regionList) {
		this.regionList = regionList;
	}

	public List<LocationCategories> getCountryList() {
		return countryList;
	}

	public void setCountryList(List<LocationCategories> countryList) {
		this.countryList = countryList;
	}

}
